#include "KeyRead.h"

#define KEY_NUM_MAX 26
#define KEYBOARD_NUM 20
#define FUN_KEY_NUM 6

#define KEY_BUF_L 0
#define KEY_BUF_R 1
#define KEY_BUF_CURRENT 0
#define KEY_BUF_LAST 1

#define KEY_VALUE_NO 0

GPIO_TypeDef *KeyboardPortTab[KEY_NUM_MAX] = {
    KEY1_GPIO_Port,
    KEY2_GPIO_Port,
    KEY3_GPIO_Port,
    KEY4_GPIO_Port,
    KEY5_GPIO_Port,
    KEY6_GPIO_Port,
    KEY7_GPIO_Port,
    KEY8_GPIO_Port,
    KEY9_GPIO_Port,
    KEY10_GPIO_Port,
    KEY11_GPIO_Port,
    KEY12_GPIO_Port,
    KEY13_GPIO_Port,
    KEY14_GPIO_Port,
    KEY15_GPIO_Port,
    KEY16_GPIO_Port,
    KEY17_GPIO_Port,
    KEY18_GPIO_Port,
    KEY19_GPIO_Port,
    KEY20_GPIO_Port,

    KEY_FP_GPIO_Port,
    KEY_FM_GPIO_Port,
    KEY_TIMBRE_GPIO_Port,
    KEY_DITHER_GPIO_Port,
    KEY_VP_GPIO_Port,
    KEY_VM_GPIO_Port,
};

uint32_t KeyboardPinTab[KEY_NUM_MAX] = {
    KEY1_Pin,
    KEY2_Pin,
    KEY3_Pin,
    KEY4_Pin,
    KEY5_Pin,
    KEY6_Pin,
    KEY7_Pin,
    KEY8_Pin,
    KEY9_Pin,
    KEY10_Pin,
    KEY11_Pin,
    KEY12_Pin,
    KEY13_Pin,
    KEY14_Pin,
    KEY15_Pin,
    KEY16_Pin,
    KEY17_Pin,
    KEY18_Pin,
    KEY19_Pin,
    KEY20_Pin,

    KEY_FP_Pin,
    KEY_FM_Pin,
    KEY_TIMBRE_Pin,
    KEY_DITHER_Pin,
    KEY_VP_Pin,
    KEY_VM_Pin,
};

volatile uint32_t KeyReadTick = 0;
uint8_t KeyValue[2][2] = {0, 0, 0, 0};
uint8_t KeyboardValueL = 0;
uint8_t KeyboardValueR = 0;
uint8_t FunKeyValueL = 0;
uint8_t FunKeyValueR = 0;

void KeyReadInit(void)
{
  KeyReadTick = HAL_GetTick();
  HAL_GPIO_WritePin(KEY_OUT_R_GPIO_Port, KEY_OUT_R_Pin, GPIO_PIN_RESET);
  HAL_GPIO_WritePin(KEY_OUT_L_GPIO_Port, KEY_OUT_L_Pin, GPIO_PIN_RESET);
}

void KeyReadProcess(void)
{
  uint8_t i = 0;
  if (HAL_GetTick() - KeyReadTick > 10)
  {
    KeyReadTick = HAL_GetTick();

    HAL_GPIO_WritePin(KEY_OUT_L_GPIO_Port, KEY_OUT_L_Pin, GPIO_PIN_SET);
    HAL_GPIO_WritePin(KEY_OUT_R_GPIO_Port, KEY_OUT_R_Pin, GPIO_PIN_RESET);
    KeyValue[KEY_BUF_LAST][KEY_BUF_L] = KeyValue[KEY_BUF_CURRENT][KEY_BUF_L];
    KeyValue[KEY_BUF_CURRENT][KEY_BUF_L] = KEY_VALUE_NO;
    for (i = 0; i < KEY_NUM_MAX; i++)
    {
      if (HAL_GPIO_ReadPin(KeyboardPortTab[i], KeyboardPinTab[i]) == GPIO_PIN_SET)
      {
        KeyValue[KEY_BUF_CURRENT][KEY_BUF_L] = i + 1;
        break;
      }
    }

    HAL_GPIO_WritePin(KEY_OUT_L_GPIO_Port, KEY_OUT_L_Pin, GPIO_PIN_RESET);
    HAL_GPIO_WritePin(KEY_OUT_R_GPIO_Port, KEY_OUT_R_Pin, GPIO_PIN_SET);
    KeyValue[KEY_BUF_LAST][KEY_BUF_R] = KeyValue[KEY_BUF_CURRENT][KEY_BUF_R];
    KeyValue[KEY_BUF_CURRENT][KEY_BUF_R] = KEY_VALUE_NO;
    for (i = 0; i < KEY_NUM_MAX; i++)
    {
      if (HAL_GPIO_ReadPin(KeyboardPortTab[i], KeyboardPinTab[i]) == GPIO_PIN_SET)
      {
        KeyValue[KEY_BUF_CURRENT][KEY_BUF_R] = i + 1;
        break;
      }
    }

    if (KeyValue[KEY_BUF_LAST][KEY_BUF_L] == KeyValue[KEY_BUF_CURRENT][KEY_BUF_L])
    {
      KeyboardValueL = KEY_VALUE_NO;
      FunKeyValueL = KEY_VALUE_NO;
      if (KeyValue[KEY_BUF_CURRENT][KEY_BUF_L] <= KEYBOARD_NUM)
      {
        KeyboardValueL = KeyValue[KEY_BUF_CURRENT][KEY_BUF_L];
      }
      else
      {
        FunKeyValueL = KeyValue[KEY_BUF_CURRENT][KEY_BUF_L] - KEYBOARD_NUM;
      }
    }

    if (KeyValue[KEY_BUF_LAST][KEY_BUF_R] == KeyValue[KEY_BUF_CURRENT][KEY_BUF_R])
    {
      KeyboardValueR = KEY_VALUE_NO;
      FunKeyValueR = KEY_VALUE_NO;
      if (KeyValue[KEY_BUF_CURRENT][KEY_BUF_R] <= KEYBOARD_NUM)
      {
        KeyboardValueR = KeyValue[KEY_BUF_CURRENT][KEY_BUF_R];
      }
      else
      {
        FunKeyValueR = KeyValue[KEY_BUF_CURRENT][KEY_BUF_R] - KEYBOARD_NUM;
      }
    }
  }
}

uint8_t GetKeyboardValueL(void)
{
  return KeyboardValueL;
}
uint8_t GetKeyboardValueR(void)
{
  return KeyboardValueR;
}

uint8_t GetFunKeyValueL(void)
{
  return FunKeyValueL;
}
uint8_t GetFunKeyValueR(void)
{
  return FunKeyValueR;
}
